// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Large Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_LARGEPLATFORM

// Function declarations
reserve function LargePlatform_Function95
reserve function LargePlatform_DebugDraw
reserve function LargePlatform_DebugSpawn

// Static Values
public value LargePlatform_value29 = 0;

// Tables
public table LargePlatform_table27
	-32, -32, -32, -32, -32, -32, -32, -32, -33, -34, -35, -36, -37, -38, -39, -40, -41, -42, -43, -44, -45, -46, -47, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -32, -32, -32, -32, -32, -32, -32
end table

public table LargePlatform_table28
	-33, -34, -35, -36, -37, -38, -39, -40, -41, -42, -43, -44, -45, -46, -47, -48, -49, -50, -51, -52, -53, -54, -55, -56, -57, -58, -59, -60, -61, -62, -63, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49
end table

private table LargePlatform_table25
	0, 0, 0, 0, 2, 2, 2, 2, 1, 
end table

private table LargePlatform_table26
	1, 2, 3, 4, 1, 2, 3, 4, 5
end table

function LargePlatform_Function95
	if temp0 == 0
		switch object[arrayPos0].state
		case 1
			object[arrayPos0].ypos -= 0x100000
			object[arrayPos0].value4 = 0x800
			break
		case 2
			object[arrayPos0].ypos -= 0x180000
			object[arrayPos0].value4 = 0xC00
			break
		case 3
			object[arrayPos0].ypos -= 0x200000
			object[arrayPos0].value4 = 0x1000
			break
		case 4
			object[arrayPos0].ypos -= 0x300000
			object[arrayPos0].value4 = 0x1800
			break
		end switch
	else
		switch object[arrayPos0].state
		case 1
			object[arrayPos0].ypos -= 0x100000
			object[arrayPos0].value4 = -0x800
			break
		case 2
			object[arrayPos0].ypos -= 0x180000
			object[arrayPos0].value4 = -0xC00
			break
		case 3
			object[arrayPos0].ypos -= 0x200000
			object[arrayPos0].value4 = -0x1000
			break
		case 4
			object[arrayPos0].ypos -= 0x300000
			object[arrayPos0].value4 = -0x1800
			break
		end switch
	end if
end function


function LargePlatform_DebugDraw
	temp0 = object[0].value17
	temp0 -= LargePlatform_value29
	GetTableValue(temp1, temp0, LargePlatform_table25)
	DrawSprite(temp1)
end function


function LargePlatform_DebugSpawn
	temp0 = object[0].value17
	temp0 -= LargePlatform_value29
	CreateTempObject(TypeName[Large Platform], 0, object.xpos, object.ypos)
	GetTableValue(object[tempObjectPos].frame, temp0, LargePlatform_table25)
	GetTableValue(object[tempObjectPos].state, temp0, LargePlatform_table26)
	arrayPos0 = object[tempObjectPos].entityPos
	temp0 = object.direction
	CallFunction(LargePlatform_Function95)
end function


event ObjectMain
	switch object.state
	case 0
		object.angle = 0
		object.value4 = 0
		break
	case 1
		object.angle = oscillation
		object.angle <<= 1
		object.angle += 16
		break
	case 2
		object.angle = oscillation
		object.angle <<= 1
		object.angle += 48
		break
	case 3
		object.angle = oscillation
		object.angle <<= 1
		object.angle += 80
		break
	case 4
		object.angle = oscillation
		object.angle <<= 1
		object.angle += 112
		break
	case 5
		if object.value0 == 1
			if object.angle < 128
				object.angle += 8
			end if
		else
			if object.angle > 0
				object.angle -= 4
			end if
		end if
		object.value4 = 0x800
		if object.angle > 80
			if object.value3 == 0
				object.value3 = 1
				CreateTempObject(TypeName[Grass Fireball], 0, object.xpos, object.ypos)
				if object.frame < 2
					object[tempObjectPos].xpos -= 0x380000
				else
					object[tempObjectPos].xpos -= 0x180000
				end if
				object[tempObjectPos].value1 = object.entityPos
				object[tempObjectPos].value2 = 8
				object[tempObjectPos].drawOrder = 5
				object[tempObjectPos].value3 = object.frame
			end if
		end if
		break
	end switch
	Sin(object.value2, object.angle)
	object.value2 *= object.value4
	object.value2 += object.ypos
	object.value2 &= -0x10000
	object.value2 -= object.value1
	object.value0 = 0
	temp0 = object.ypos
	object.ypos = object.value1
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		switch object.frame
		case 0
			temp1 = object[currentPlayer].xpos
			temp1 -= object.xpos
			temp1 >>= 17
			temp1 += 32
			if temp1 < 0
				temp1 = 0
			end if
			if temp1 > 63
				temp1 = 63
			end if
			GetTableValue(temp2, temp1, LargePlatform_table27)
			BoxCollisionTest(C_BOX, object.entityPos, -64, temp2, 64, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			temp2 -= 3
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, temp2, 64, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ypos += 0x40000
				object[currentPlayer].ypos += object.value2
				object.value0 = 1
			end if
			break
		case 1
			temp1 = object[currentPlayer].xpos
			temp1 -= object.xpos
			temp1 >>= 17
			temp1 += 32
			if temp1 < 0
				temp1 = 0
			end if
			if temp1 > 63
				temp1 = 63
			end if
			GetTableValue(temp2, temp1, LargePlatform_table28)
			BoxCollisionTest(C_BOX, object.entityPos, -64, temp2, 64, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			temp2 -= 3
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, temp2, 64, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ypos += 0x40000
				object[currentPlayer].ypos += object.value2
				object.value0 = 1
			end if
			break
		case 2
			BoxCollisionTest(C_BOX, object.entityPos, -32, -47, 32, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			temp2 -= 3
			BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -51, 32, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ypos += 0x40000
				object[currentPlayer].ypos += object.value2
				object.value0 = 1
			end if
			break
		end switch
	next
	object.ypos = temp0
	Sin(object.value1, object.angle)
	object.value1 *= object.value4
	object.value1 += object.ypos
	object.value1 &= -0x10000
end event


event ObjectDraw
	DrawSpriteXY(object.frame, object.xpos, object.value1)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-64, -48, 128, 80, 1, 324)
	SpriteFrame(-64, -64, 128, 96, 317, 396)
	SpriteFrame(-32, -48, 64, 96, 446, 396)
	foreach (TypeName[Large Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = object[arrayPos0].propertyValue
		object[arrayPos0].frame >>= 4
		object[arrayPos0].frame %= 3
		object[arrayPos0].state = object[arrayPos0].propertyValue
		object[arrayPos0].state &= 7
		if object[arrayPos0].state > 5
			object[arrayPos0].state = 0
		end if
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 15
		temp0 >>= 3
		CallFunction(LargePlatform_Function95)
		object[arrayPos0].drawOrder = 5
	next
	temp0 = 0
	LargePlatform_value29 = DebugMode_ObjCount
	while temp0 < 9
		SetTableValue(TypeName[Large Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(LargePlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(LargePlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
